package com.wf.config;

import com.wf.interceptor.JwtTokenUserInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * 配置WebMvcConfigurer的实现类，用于注册拦截器
 */
@Configuration
@Slf4j
public class    WebMvcConfiguration implements WebMvcConfigurer {

    /**
     * 注入JwtTokenUserInterceptor实例，用于处理JWT令牌用户认证
     */
    @Autowired
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;

    /**
     * 添加拦截器，配置拦截路径和排除路径
     * @param registry 拦截器注册对象
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册jwtTokenUserInterceptor拦截器，并配置拦截路径和排除路径
        registry.addInterceptor(jwtTokenUserInterceptor)
                .addPathPatterns("/app/**","/agency/process")
                .excludePathPatterns("/app/user/**", "/app/house/page", "/app/apartment/**"
                ,"/app/banner/info/list","app/message");
    }

    /**
     * 扩展消息转换器。
     *
     * @param converters 消息转换器列表。
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("消息转换器启动...");
        // 创建一个MappingJackson2HttpMessageConverter实例
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();

        // 设置自定义的JacksonObjectMapper作为转换器的ObjectMapper

        converter.setObjectMapper(new JacksonObjectMapper());

        // 将转换器添加到转换器列表的首位
        converters.add(0, converter);
    }

}
